import { $, updateFloat } from "../../utils";

export const RedPacketType = {
    0: `粉丝包`,
    1: `金粉`,
    5: `粉丝包`,
    2: `快币包`,
    3: `倒计时`,
    4: `评论包`,
    6: `评论包`,
    7: `人气票`,
    8: `穿云箭红包`,
    9: `锦鲤奖`,
    10: `分享包`
}
export class RedPacket {
    private __text: string;
    private __join: UiObject;
    /**
     * 类型
     * 0 粉丝包 开奖时留在直播间，更容易抢到
     * 5 粉丝包 加入主播粉丝团，即可参与抢红包
     * 1 金粉
     * 2 快币包 开奖时需要留在直播间
     * 3 倒计时 快币红包
     * 4 评论包 在评论区发送口令，即可参与抢红包
     * 6 评论包 开奖前留在直播间，更容易抢到
     * 7 人气票
     * 8 穿云箭红包
     * 9 锦鲤奖 关注主播抢红包
     * 10 分享包
     */
    type: number;
    /**
     * 大小
     */
    count: number;
    /**
     * 总数
     */
    total: number;
    /**
     * 1 已参加
     * 0 未参加
     */
    status: number;
    /**
     * 进度
     */
    process: string;
    /**
     * 口令
     */
    password: string;

    toJson() {
        return {
            type: this.type,
            total: this.total,
            status: this.status,
            process: this.process,
            password: this.password,
            count: this.count,
            text: this.__text,
        }
    }
    setText(text: string) {
        /**
         * 完成任务可瓜分红包 0/2 120 快币 开金粉 抢红包 距任务结束还有 01 : 34
         * 主播的粉丝团红包 4 x25 快币 11分钟后开奖 开奖时留在直播间，更容易抢到
         */
        this.__text = text;
        if (this.checkJinFen(text)) {
            return;
        }
        if (this.checkFans(text)) {
            return;
        }
        if (this.checkKwaibi(text)) {
            return;
        }
        if (this.checkTime(text)) {
            return;
        }
        if (this.checkComment(text)) {
            return;
        }
        if (this.checkComment2(text)) {
            return;
        }
        if (this.checkFans2(text)) {
            return;
        }
        if (this.checkRenqi(text)) {
            return;
        }
        if (this.checkChuan(text)) {
            return;
        }
        if (this.checkJinli(text)) {
            return;
        }
        if (this.checkShare(text)) {
            return;
        }
        updateFloat({ tip: this.__text })
        log(this.__text)
    }
    getText() {
        return this.__text;
    }

    private checkShare(text: string) {
        const res = text.match(/主播的分享红包(.*?)x(.*?)快币(.*?)分享直播间即可参与抢红包/)
        if (res && res.length > 0) {
            const [, count, total, status] = res;
            this.count = parseInt(count.trim())
            this.total = parseInt(total.trim())
            if (/.*?后开奖/.test(status)) {
                this.status = 1
            } else {
                this.status = 0;
            }
            this.type = 10;
            return true;
        }
        return false;
    }

    private checkJinli(text: string) {
        const res = text.match(/.*?开启星光热门红包\s关注主播抢红包x(.*?)x(.*?)送活动礼物抢锦鲤奖/)
        if (res && res.length > 0) {
            const [, count, total] = res;
            this.count = parseInt(count.trim())
            this.total = parseInt(total.trim())
            this.status = 0;
            this.type = 9;
            return true;
        }
        return false;
    }

    private checkChuan(text: string) {
        // 二十四的穿云箭红包 288 快币 抢红包 开奖前留在直播间，更容易抢到
        const res = text.match(/.*?穿云箭红包(.*?)快币(.*?)开奖前留在直播间，更容易抢到/)
        if (res && res.length > 0) {
            const [, count, status] = res;
            this.count = parseInt(count.trim())
            if (/.*?后开奖/.test(status)) {
                this.status = 1
            } else {
                this.status = 0;
            }
            this.type = 8;
            return true;
        }
        return false;
    }

    private checkRenqi(text: string) {
        const res = text.match(/.*?人气.*?开红包(.*?)x(.*?)x(.*?)送人气票\s抢红包/)
        if (res && res.length > 0) {
            const [, process, count, piao, status] = res;
            this.count = parseInt(count.trim())
            this.password = piao.trim();
            this.process = process.trim()
            if (/.*?后开奖/.test(status)) {
                this.status = 1
            } else {
                this.status = 0;
            }
            this.type = 7;
            return true;
        }
        return false;
    }

    private checkComment(text: string) {
        const res = text.match(/主播的口令红包(.*?)快币.口令：(.*?)(.*?)在评论区发送口令，即可参与抢红包/)
        if (res && res.length > 0) {
            const [, count, password, status] = res;
            this.count = parseInt(count.trim())
            this.password = password.trim();
            if (/.*?后开奖/.test(status)) {
                this.status = 1
            } else {
                this.status = 0;
            }
            this.type = 4;
            return true;
        }
        return false;
    }
    private checkComment2(text: string) {
        const res = text.match(/主播的口令红包(.*?)快币 口令：(.*?)\s(.*?)开奖前留在直播间，更容易抢到/)
        if (res && res.length > 0) {
            const [, count, password, status] = res;
            this.count = parseInt(count.trim())
            this.password = password.trim();
            if (/.*?后开奖/.test(status)) {
                this.status = 1
            } else {
                this.status = 0;
            }
            this.type = 6;
            return true;
        }
        return false;
    }

    private checkTime(text: string) {
        // 忍8858...的快币红包 60 快币 关注TA，更容易抢到红包
        const res = text.match(/.*?的快币红包(.*?)快币\s关注TA，更容易抢到红包/)
        if (res && res.length > 0) {
            const [, count] = res;
            this.count = parseInt(count.trim())
            this.status = 0;
            this.type = 3;
            return true;
        }
        return false;
    }

    private checkKwaibi(text: string) {
        const res = text.match(/主播的快币红包(.*?)快币(.*?)开奖时需要留在直播间/)
        if (res && res.length > 0) {
            const [, count, status] = res;
            this.count = parseInt(count.trim())
            if (/.*?后开奖/.test(status)) {
                this.status = 1
            } else {
                this.status = 0;
            }
            // todo 判断金粉 红包参与状态
            this.type = 2;
            return true;
        }
        return false;
    }

    private checkJinFen(text: string) {
        const res = text.match(/完成任务可瓜分红包\s(.*?)\s(.*?)快币(.*?)距任务结束还有/)
        if (res && res.length > 0) {
            const [, process, count, status] = res;
            this.process = process.trim()
            this.count = parseInt(count.trim())
            this.status = 0;
            // todo 判断金粉 红包参与状态
            this.type = 1;
            return true;
        }
        return false;
    }

    private checkFans(text: string) {
        const res = text.match(/主播的粉丝团红包(.*?)x(.*?)快币(.*?)开奖时留在直播间，更容易抢到/)
        if (res && res.length > 0) {
            const [, count, total, status] = res;
            this.count = parseInt(count.trim())
            this.total = parseInt(total.trim())
            if (/.*?后开奖/.test(status)) {
                this.status = 1
            } else {
                this.status = 0;
            }
            this.type = 0;
            return true;
        }
        return false;
    }

    private checkFans2(text: string) {
        const res = text.match(/主播的粉丝团红包(.*?)x(.*?)快币(.*?)加入主播粉丝团，即可参与抢红包/)
        if (res && res.length > 0) {
            const [, count, total, status] = res;
            this.count = parseInt(count.trim())
            this.total = parseInt(total.trim())
            if (/.*?后开奖/.test(status)) {
                this.status = 1
            } else {
                this.status = 0;
            }
            this.type = 5;
            return true;
        }
        return false;
    }

    setJoinButton(btn: UiObject) {
        this.__join = btn;
    }

    join(isJoin: boolean = true) {
        log(`红包类型为：`, this.type)
        if (this.status == 1) return true;
        // 普通红包
        if (this.type === 2 || this.type === 8) {
            if (!isJoin) return true;
            this.joinDefault()
            return true;
        }
        // 评论包
        if (this.type === 4 || this.type === 6) {
            if (!isJoin) return true;
            this.joinComment()
            return true;
        }
        if (this.type === 0 || this.type === 5) {
            // 粉丝煲
            if (!isJoin) return true;
            // this.joinFans()
            return false;
        }
        if (this.type === 10) {
            // 分享包
            if (!isJoin) return true;
            return false;
        }
        return false;
    }

    joinComment() {
        $.tryClickButton(this.__join)
        $.tryClickButton(selector().text('发送').visibleToUser(true).findOne())
        // 结束后点开红包
        $.tryClickButton(selector().idEndsWith("live_red_packet_status_text").visibleToUser(true).findOne(5 * 1000))
    }

    joinFans() {
        $.tryClickButton(this.__join)
        // 结束后点开红包
        $.tryClickButton(selector().idEndsWith("live_red_packet_status_text").visibleToUser(true).findOne(5 * 1000))
    }

    joinDefault() {
        $.tryClickButton(this.__join)
    }
}